package com.kitty.game.fixed.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.confirm.service.handler.ConfirmHandler;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.fixed.entity.FixedTeam;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.message.ReqConfirmResult;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;

@Component
@Slf4j
public class QuitFixedTeamConfirmHandler extends ConfirmHandler {
    @Autowired
    Dao dao;

    @Override
    public void handleConfirmResult(Role role, ReqConfirmResult reqConfirmResult) {
        log.info("点击了退出固定队确定");
        if ("1".equals(reqConfirmResult.getSelect())) {
            goToQuitFixedTeam(role);
        }
    }

    /**
     * 确认退出固定队
     */
    private void goToQuitFixedTeam(Role role) {
        String fixedTeamId = role.getFixedTeam();
        if(StringUtils.isEmpty(fixedTeamId)){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("没有固定队"));
            return;
        }
        List<FixedTeam> fixedList = dao.query(FixedTeam.class, Cnd.where("uid", "=", fixedTeamId));
        FixedTeam fixedTeam = new FixedTeam();
        if(fixedList.size()>0){
            fixedTeam = fixedList.get(0);
            if(role.getRoleId() == Integer.parseInt(fixedTeam.getLeader_uid())){
                //如果 选择退出的人是队长。 要解散固定队
                //取固定队中所有人的ID
                JSONArray parseArray = JSONObject.parseArray(fixedTeam.getMembers());
                for (Object o : parseArray) {
                    JSONObject jsonObject = (JSONObject)o;
                    int roleId = Integer.parseInt(jsonObject.getString("gid"));
                    Role teamRole = SpringUtils.getRoleService().getPlayerBy(roleId);
                    teamRole.setFixedTeam("");
                    teamRole.save();
                    MessagePusher.pushMessage(teamRole,new RespNotifyMiscEx("队长#Y"+role.getName()+"#n已解散固定队伍!"));
                    log.info("已解散固定队伍!"+teamRole.getName()+"已退出");
                }
                dao.delete(fixedTeam);
            }else{
                //不是队长
                JSONArray parseArray = JSONObject.parseArray(fixedTeam.getMembers());
                Iterator<Object> iterator = parseArray.iterator();
                while(iterator.hasNext()) {
                    JSONObject jsonObject = (JSONObject)iterator.next();
                    int roleId = Integer.parseInt(jsonObject.getString("gid"));
                    Role teamRole = SpringUtils.getRoleService().getPlayerBy(roleId);
                    if(role.getRoleId() == roleId){
                        iterator.remove();
                        teamRole.setFixedTeam("");
                        teamRole.save();
                    }
                    MessagePusher.pushMessage(teamRole,new RespNotifyMiscEx("队员#Y"+role.getName()+"#n已退出固定队伍!"));
                }
                fixedTeam.setMembers(parseArray.toJSONString());
                dao.insertOrUpdate(fixedTeam);
            }
        }

    }
}
